from marshmallow.fields import Decimal
from marshmallow_sqlalchemy import auto_field

from applications.extensions import ma
from marshmallow import fields, post_dump
from applications.models import FundStockListCwzx
from applications.models import FundIndexEva


def get_data(obj):
    if hasattr(obj, "data"):
        return obj.data
    return None


class FundStockCWZXSchema(ma.SQLAlchemyAutoSchema):
    # xq_url = auto_field("xq_symbol", dump_only=True)

    class Meta:
        model = FundStockListCwzx
        # 反序列化阶段时，直接返回模型对象
        load_instance = True
        exclude = ('fund_code', 'fund_name', 'create_time', 'update_time')

    @post_dump
    def post_dump(self, data, **kwargs):
        """序列化的后置钩子，会在数据转换之后执行"""
        # code = data['symbol']
        # subCode = code[0:2]
        # if subCode == '60' or subCode == '68' or subCode == '90':
        #     code = 'SH' + code
        # else:
        #     code = 'SZ' + code
        # xq_url = 'https://xueqiu.com/S/' + code
        # data["xq_url"] = xq_url

        return data


class FundIndexEvaSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = FundIndexEva
        # 反序列化阶段时，直接返回模型对象
        load_instance = True

    @post_dump
    def post_dump(self, data, **kwargs):
        """序列化的后置钩子，会在数据转换之后执行"""
        return data
